//
//  LCHistoryDetailsTableViewController.m
//  JKFuYinProject
//
//  Created by jiukuyinyue on 2017/10/14.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCHistoryDetailsTableViewController.h"
#import "LCHistoryDetailsTableViewController+LCAVPlay.h"
#import "LCViewController+LCMorePicturesTable.h"

#define HistoryDetailsCellID @"HistoryDetailsCellID"
@interface LCHistoryDetailsTableViewController ()
<UITableViewDelegate,UITableViewDataSource>

@end

@implementation LCHistoryDetailsTableViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [LCFloatingWindow sharedInstance].hidden = NO;//安装AVPlay
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [LCFloatingWindow sharedInstance].hidden = YES;//隐藏AVPlay
}

- (void)dealloc
{
    [self baiduAdDealloc];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"历史记录";
    self.songsData = [NSArray array];
    [self creareAVPlay];

    [self loadData];
    //添加以下代码关闭估算行高
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:HistoryDetailsCellID];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnAction)];

    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshGifHeader *refreshGifHeader = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 隐藏时间
    refreshGifHeader.lastUpdatedTimeLabel.hidden = YES;
    // 设置header
    self.tableView.mj_header = refreshGifHeader;
     //如果没有占满则清除下面的
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    
    [self setupTableViewLayout];

    //加载组图广告
    [self pressToLoadAdForTableView:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
}

- (void)setupTableViewLayout
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-0);
        }
    }];
}

- (void)setupTableViewCompatibilityADLayout
{
    CGFloat width = MIN(self.view.width , self.view.height);
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-width/20*3);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-width/20*3);
        }
    }];
}

-(void)loadData{
    
    self.songsData = [[[LCHistoryDataManager loadTenSongsData] reverseObjectEnumerator] allObjects];
}

-(void)loadNewData{
    [self loadData];
    [self.tableView reloadData];
  
    [self.tableView.mj_header endRefreshing];
}
-(void)rightBtnAction{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TENSONGS"];
    _LCPlayer.selectedData = nil;
    if ([LCHistoryDataManager loadTenSongsData].count > 0) {
        self.songsData = [[[LCHistoryDataManager loadTenSongsData] reverseObjectEnumerator] allObjects];
    }else{
        self.songsData = [NSArray array];
    }
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.morePicturesAd.adViewArray.count > 0) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.songsData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && self.morePicturesAd.adViewArray.count > 0) {
       return LCPicsHeight-50;
    }
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.songsData.count > 8) {
        return 60;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!cell) {
        cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"foot"];
    }
    cell.backgroundColor = RGBColor(236, 236, 236);
    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.morePicturesAd.adViewArray.count > 0 && indexPath.section == 0)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCellIdentifier"];
        }
        BaiduMobAdNativeAdView *view = [self.morePicturesAd.adViewArray objectAtIndex:indexPath.row];
        view.tag = 0;
        [[cell viewWithTag:0] removeFromSuperview];
        view.frame =
        CGRectMake(10, 15, [UIScreen mainScreen].bounds.size.width-10, LCPicsHeight);
        UIView *bgView = [[UIView alloc] init];
        bgView.frame =
        CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, LCPicsHeight-50);
        bgView.clipsToBounds = YES;
        [cell.contentView addSubview:bgView];
        [bgView addSubview:view];
        [self sendVisibleImpressionAtIndexPath:indexPath tableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    LSZAudioFrequencyTableViewCell * cell = [[LSZAudioFrequencyTableViewCell alloc] initWithNumberAudioFrequency:0];
    cell.labelChapters.frame = CGRectMake(44,CGRectGetMaxY(cell.labelTitle.frame)+8, WidthScal_320(180), 12);
    cell.labelTitle.text = [NSString stringWithFormat:@"%@", self.songsData[indexPath.row][@"title"]];
    cell.labelChapters.text = [NSString stringWithFormat:@"%@", self.songsData[indexPath.row][@"singerName"]];
    cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    cell.butShare.hidden = YES;
//    [cell.butShare addTarget:self action:@selector(clickShare:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
//#pragma mark - 分享  下载
//-(void)clickShare:(UIButton *)button
//{
//    NSLog(@"分享  下载");
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [LSZSpecialPackaging addPlaylistWithArrayData:self.songsData];
    LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
    _LCPlayer.songs = playlistTest.allSongs;
    if (_LCPlayer.songs != nil) {
        [_LCPlayer playAtIndex:indexPath.row isSelectPlay:YES];
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
